<template>
	<scroll-view :enable-flex="true" :show-scrollbar="false" @scrolltolower="lower" :scroll-y="true" style="width: 100%;height: 100vh; display: flex;flex-direction: column; align-items: center;">
		<!-- 好友item -->
		<view style="width: 702rpx; display: flex;flex-direction: column;align-items: center;justify-content: center;margin-top: 30rpx;"
		 v-for="(item,index) in friendList" :key="index">
			<view style="width: 702rpx;display: flex;justify-content: space-between;align-items: center;" @click="call(item.mobile)">
				<view style="display: flex;">
					<image mode="aspectFill" :src="item.avatar" style="width: 90rpx;height: 90rpx;border-radius: 50%;"></image>
					<view style="display: flex;flex-direction: column;justify-content: space-between; margin-left: 23rpx;">
						<!-- 昵称 -->
						<text style="font-size: 28rpx;color: #333333;">{{item.user_nickname}}</text>
						<!-- 关系 -->
						<text style="font-size: 28rpx;color: #666666;">{{item.mobile}}</text>
					</view>
				</view>
				<text style="font-size: 20rpx;color: #808080;align-self: flex-start;">{{item.create_time}}</text>
			</view>
			<view style="width: 100%;height: 1px;background-color: #F0F2F5;margin-top: 30rpx;"></view>
		</view>
		<view v-if="friendList.length < 1" style="font-size: 22rpx;color: #666666;margin-top: 40rpx;">当前没有好友，快去分享吧!</view>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				id: '',
				friendList: [{
					/**
					 * id	用户id
					 * user_nickname	昵称
					 * avatar	头像
					 * create_time	注册时间
					 */
				}]
			}
		},
		onLoad() {
			// 取出用户id
			let that = this;
			uni.getStorage({
				key: 'user',
				success(res) {
					that.id = res.data.id;
					//获取数据
					that.getMyfriend(0);
				},
				fail() {
					console.log('没有用户信息,去登陆');
					that.$Router.push({
						name: 'login'
					})
				}
			});
		},
		methods: {
			async call(e){
				 if (e) {
				 	uni.makePhoneCall({
				 	    phoneNumber: e //仅为示例
				 	});
				 }else{
					 uni.$showMsg('暂无手机号码!') 
				 }
			},
			lower() {
				console.log("到达底部了");
				this.page += 1;
				this.getMyfriend(1);
			},
			async getMyfriend(style) {
				//如果是初始化数据，就把page重置
				if (style == 0) this.page = 1;
				// 获取教练数据
				let res;
				try {
					res = await this.$request.post("api/User/buddies", {
						data: {
							uid: this.id,
							page: this.page,
							limit: this.limit
						}
					});
				} catch (e) {
					//TODO handle the exception
					return uni.showToast({
						title: "网络连接失败！",
						icon: "none"
					});
				}

				console.log('好友', res.data.data);
				// 把新数据追加到当前数组列为1,初始化数据为0
				if (style == 1) {
					this.friendList = this.friendList.concat(res.data.data);
				} else {
					this.friendList = res.data.data;
				}

			}
		}
	}
</script>

<style>

</style>
